﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using WeManageService;

namespace WeManageForm.Report
{
    public partial class ReportForm : Form
    {
        DateTime StartTime = DateTime.Today.AddDays(-7);
        DateTime EndTime = DateTime.Today;
        int type = 0;

        public ReportForm()
        {
            InitializeComponent();
            lblAllFinishPlan.Text = Reporter.GetAllFinishPlan().ToString();
            lblAllScore.Text = Reporter.GetScore().ToString();
            lblAllFinishWish.Text = Reporter.GetAllFinishWish().ToString();
            lblAllUsedScore.Text = Reporter.GetUsedScore().ToString();
            lblTodayFinishPlan.Text = Reporter.GetFinishPlanByTime(DateTime.Today,DateTime.Today.AddDays(1)).ToString();
            lblTodayScore.Text = Reporter.GetScoreByTime(DateTime.Today, DateTime.Today.AddDays(1)).ToString();
            lblTodayFinishWish.Text = Reporter.GetFinishWishByTime(DateTime.Today, DateTime.Today.AddDays(1)).ToString();
            lblTodayUsedScore.Text = Reporter.GetUsedScoreByTime(DateTime.Today, DateTime.Today.AddDays(1)).ToString();
            dtpStartTime.Value = StartTime;
            dtpEndTime.Value = EndTime;
            InitChart();
        }

        private void btnShowFinishPlan_Click(object sender, EventArgs e)//type0
        {
            StartTime = dtpStartTime.Value.Date;
            EndTime = dtpEndTime.Value.Date;
            if (StartTime >= EndTime)
            {
                MessageBox.Show("结束日期必须晚于开始日期！", "日期错误");
            }
            else
            {
                type = 0;
                InitChart();
            }
        }

        private void btnShowUnfinishPlan_Click(object sender, EventArgs e)//type1
        {
            StartTime = dtpStartTime.Value.Date;
            EndTime = dtpEndTime.Value.Date;
            if (StartTime >= EndTime)
            {
                MessageBox.Show("结束日期必须晚于开始日期！", "日期错误");
            }
            else
            {
                type = 1;
                InitChart();
            }
        }

        private void btnWish_Click(object sender, EventArgs e)//type2
        {
            StartTime = dtpStartTime.Value.Date;
            EndTime = dtpEndTime.Value.Date;
            if (StartTime >= EndTime)
            {
                MessageBox.Show("结束日期必须晚于开始日期！", "日期错误");
            }
            else
            {
                type = 2;
                InitChart();
            }
        }

        private void btnScore_Click(object sender, EventArgs e)//type3
        {
            StartTime = dtpStartTime.Value.Date;
            EndTime = dtpEndTime.Value.Date;
            if (StartTime >= EndTime)
            {
                MessageBox.Show("结束日期必须晚于开始日期！", "日期错误");
            }
            else
            {
                type = 3;
                InitChart();
            }
        }

        private void InitChart()
        {
            //定义图表区域
            this.chart1.ChartAreas.Clear();
            ChartArea chartArea1 = new ChartArea("C1");
            this.chart1.ChartAreas.Add(chartArea1);
            //定义存储和显示点的容器
            this.chart1.Series.Clear();
            Series series1 = null;
            Series series2 = null;
            switch (type)
            {
                case 0:
                    series1 = new Series("完成任务数量");
                    break;
                case 1:
                    series1 = new Series("超时任务数量");
                    break;
                case 2:
                    series1 = new Series("完成愿望数量");
                    break;
                case 3:
                    series1 = new Series("获得积分");
                    series2 = new Series("消耗积分");
                    break;
                default: break;
            }
            series1.ChartArea = "C1";
            this.chart1.Series.Add(series1);
            if (type == 3) chart1.Series.Add(series2);
            //设置图表显示样式
            chart1.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.Lines;
            chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.Lines;
            chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;
            chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;//不显示竖着的分割线
            chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd";
            chart1.ChartAreas[0].AxisX.Minimum = DateTime.Parse(StartTime.ToString("yyyy-MM-dd")).ToOADate();
            chart1.ChartAreas[0].AxisX.Maximum = DateTime.Parse(EndTime.ToString("yyyy-MM-dd")).ToOADate();
            chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;
            chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("2000-01-01").Day;
            chart1.ChartAreas[0].AxisY.Interval = 1;
            this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
            //设置标题
            this.chart1.Titles.Clear();
            this.chart1.Titles.Add("S01");
            switch (type)
            {
                case 0:
                    chart1.Titles[0].Text = "任务完成情况";
                    break;
                case 1:
                    chart1.Titles[0].Text = "超时任务情况";
                    break;
                case 2:
                    chart1.Titles[0].Text = "愿望完成情况";
                    break;
                case 3:
                    chart1.Titles[0].Text = "积分变动情况";
                    break;
                default: break;
            }
            this.chart1.Titles[0].ForeColor = Color.Black;
            this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            //设置图表显示样式
            switch (type)
            {
                case 0:
                    chart1.Series[0].Color = chart1.Series[0].MarkerColor = Color.MediumAquamarine;
                    break;
                case 1:
                    chart1.Series[0].Color = chart1.Series[0].MarkerColor = Color.Red;
                    break;
                case 2:
                    chart1.Series[0].Color = chart1.Series[0].MarkerColor = Color.Violet;
                    break;
                case 3:
                    chart1.Series[0].Color = chart1.Series[0].MarkerColor = Color.BlueViolet;
                    chart1.Series[1].Color = chart1.Series[1].MarkerColor = Color.OrangeRed;
                    break;
                default: break;
            }
            this.chart1.Series[0].ChartType = SeriesChartType.Line;
            chart1.Series[0].MarkerSize = 5;
            chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
            if (type == 3)
            {
                chart1.Series[1].ChartType = SeriesChartType.Line;
                chart1.Series[1].MarkerSize = 5;
                chart1.Series[1].MarkerStyle = MarkerStyle.Circle;
            }
            //chart1.Series[0].
            this.chart1.Series[0].Points.Clear();
            this.chart1.ChartAreas[0].AxisX.Title = "                                             时间";
            this.chart1.ChartAreas[0].AxisY.Title = "数量";
            GetPoint();            
        }

        private void GetPoint()
        {
            DateTime time = StartTime;
            int data = 0;
            while (time <= EndTime)
            {
                switch(type)
                {
                    case 0:
                        data = Reporter.GetFinishPlanByTime(time, time.AddDays(1));
                        break;
                    case 1:
                        data = Reporter.GetUnfinishPlanByTime(time, time.AddDays(1));
                        break;
                    case 2:
                        data = Reporter.GetFinishWishByTime(time, time.AddDays(1));
                        break;
                    case 3:
                        data = Reporter.GetScoreByTime(time, time.AddDays(1));
                        break;
                    default:break;
                }
                if (chart1.ChartAreas[0].AxisY.Interval < data / 10)
                    chart1.ChartAreas[0].AxisY.Interval = (data / 100 + 1) * 10;
                chart1.Series[0].Points.AddXY(DateTime.Parse(time.ToString("yyyy-MM-dd")).ToOADate(), data);
                if (type == 3)
                {
                    data = Reporter.GetUsedScoreByTime(time, time.AddDays(1));
                    chart1.Series[1].Points.AddXY(DateTime.Parse(time.ToString("yyyy-MM-dd")).ToOADate(), data);
                }
                time = time.AddDays(1);
            }
        }

		private void btnWeekReport_Click(object sender, EventArgs e)
		{
			PeriodicForm form = new PeriodicForm(0);
			form.ShowDialog();
		}

		private void btnAnnualReport_Click(object sender, EventArgs e)
		{
			PeriodicForm form = new PeriodicForm(1);
			form.ShowDialog();
		}
	}
}
